<template>
  <div>
    <UploadExcel :on-success="handleSuccess" />
  </div>
</template>

<script>
import { formatExcelDate } from '@/utils/index'
import { importEmployeeApi } from '@/api/employees'
export default {
  name: '',
  data() {
    return {}
  },
  computed: {},
  created() {},
  methods: {
    handleSuccess({ results, header }) {
      console.log(results, header, 9999)
      const res = this.transExcel(results)
      this.doAdd(res)
    },
    async doAdd(res) {
      await importEmployeeApi(res)
      this.$router.push('/employees')
    },
    transExcel(results) {
      const mapInfo = {
        入职日期: 'timeOfEntry',
        手机号: 'mobile',
        姓名: 'username',
        转正日期: 'correctionTime',
        工号: 'workNumber',
        部门: 'departmentName',
        聘用形式: 'formOfEmployment'
      }
      return results.map((item) => {
        var newMap = {}
        const cnName = Object.keys(item) // ['姓名', '手机号', '入职日期', '转正日期', '工号', '部门']
        cnName.forEach((item2) => {
          const engName = mapInfo[item2] // 获取英文数据当做键
          // console.log(engName, 2)
          if (engName === 'timeOfEntry' || engName === 'correctionTime') {
            newMap[engName] = formatExcelDate(item[item2])
          } else {
            newMap[engName] = item[item2]
          }
        })
        return newMap
      })
    }
  }
}
</script>
<style lang="less" scoped></style>
